create proc [dbo].[sp_KhachHang_LoiNhuan1]
	@Start				datetime,
	@End				datetime,
	@MaKhachHang			INT
as
begin
	select	sum(cthd.gia_tien*cthd.so_luong) - sum(cthd.so_luong*hh.gia_nhap) as LoiNhuan,
	kh.ten as tenkhachhang,
	hh.ten,
	sum(cthd.gia_tien*cthd.so_luong) as TongGiaBan,
	sum(cthd.so_luong*hh.gia_nhap) as TongGiaNhap,
	sum (so_luong) as SoLuong
	from chitiethoadon as cthd
	inner join hanghoa as hh on hh.id = cthd.ma_san_pham
	inner join hoadon as hd on hd.id = cthd.ma_hoa_don
	inner join khachhang as kh on kh.id = hd.ma_khach_hang
	inner join nhomhang as nh on hh.ma_nhom_hang = nh.id
	where hd.ma_khach_hang =@MaKhachHang and  hd.ngay_lap between @Start and @End 
	group by hh.ten,kh.ten
end
